﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Threading;
using System.IO.Ports;
using System.Net;
namespace Battery
{
    public partial class Form1 : Form
    {
        //private Timer Timer;
       
        OleDbConnection conn = null;
        MasterRTU ModbusRtu = null;
        private SerialPort sp = new SerialPort();
        Thread thread_poll = null;
        Thread thread_poll2 = null;
        int m_SelectIndex = 0;
        bool m_bStart = false;
        private string strcom = "COM1";
        private string strbaud = "9600";
        private string strdatabit = "8";
        private string strparity = "None";
        private string strstopbit = "One";
        private bool m_Runing = false;
        bool m_isPortOpen = false;
        byte m_iDeviceAddr = 1;
        private bool m_DebugRight = false;
        public delegate void InsertTestRecordToList(string volt, string temp,string neizu,byte index);
        public delegate void ShowTestInfo(string volt, string curr, string temp1, string temp2, string count, string version, string strDeviceTime);
        public delegate void ShowTimeInfo(string devTime, string localTime);
        public delegate void ConfirmListCount(byte count);
        public Form1()
        {
            InitializeComponent();

            ModbusRtu = new MasterRTU();

            listView1.Columns.Add("电池序号", 80, HorizontalAlignment.Left);//向listView1中添加“文件名称”列
            listView1.Columns.Add("电压(V) ", 200, HorizontalAlignment.Left);//向listView1中添加“文件名称”列
            listView1.Columns.Add("温度(℃)", 200, HorizontalAlignment.Left);//向listView1中添加“创建时间”列
            listView1.Columns.Add("内阻(mΩ)", 200, HorizontalAlignment.Left);//向listView1中添加“创建时间”列
            listView1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular);
            listView1.Scrollable = true;//有滚动条
        }
        public string GetBeijingTime()
        {

            WebRequest request = null;
            WebResponse response = null;
            WebHeaderCollection headerCollection = null;
            string datetime = string.Empty;
            try
            {
                request = WebRequest.Create("https://www.baidu.com");
                request.Timeout = 3000;
                request.Credentials = CredentialCache.DefaultCredentials;
                response = (WebResponse)request.GetResponse();
                headerCollection = response.Headers;
                foreach (var h in headerCollection.AllKeys)
                { if (h == "Date") { datetime = headerCollection[h]; } }
                return datetime;
            }
            catch (Exception) { return datetime; }
            finally
            {
                if (request != null)
                { request.Abort(); }
                if (response != null)
                { response.Close(); }
                if (headerCollection != null)
                { headerCollection.Clear(); }
            }


        }
        private bool InitConnectDatabase()
        {
            string strModuleExcel = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            strModuleExcel = strModuleExcel + "Database" + "\\ERT.mdb";

            string CONN_STRING_STDREG32 = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;  Jet OLEDB:Database Password=pwd12345; Data Source={0}",
                     strModuleExcel);

            conn = new OleDbConnection(CONN_STRING_STDREG32);

            try
            {
                conn.Open();

            }
            catch
            {
                conn.Close();
                MessageBox.Show("数据库打开失败!");
                return false;
            }
            return true;
        }
        public string GetDataVauleToStep(string TableName, string RowName)
        {
            string sql = "select * from " + TableName;

            DataSet testdatevalue = GetItemDataSet(sql);

            if (testdatevalue == null)
            {
                return "";
            }

            string value = testdatevalue.Tables[0].Rows[0][RowName].ToString();

            return value;
        }
        private DataSet GetItemDataSet(string strCmd)
        {
            DataSet dataset = new DataSet();
            try
            {
                // conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = strCmd;
                cmd.Connection = conn;

                OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                adp.Fill(dataset);
            }
            catch
            {
                //conn.Close();
                return null;
            }

            //conn.Close();
            return dataset;
        }


        public bool UpdateTableInfo(string TableName, string wherename, string wherevalue, string RowName, string Value)
        {

            string Sql = "UPDATE " + TableName + " SET " + RowName + "='" + Value + "' where " + wherename + "='" + wherevalue + "'";

            OleDbCommand cmd = new OleDbCommand(Sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                MessageBox.Show("update error");
                return false;
            }
        }
        public bool UpdateTableInfo(string TableName, string RowName, string Value)
        {

            string Sql = "UPDATE " + TableName + " SET " + RowName + "='" + Value + "'";

            OleDbCommand cmd = new OleDbCommand(Sql, conn);
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                MessageBox.Show("update error");
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            strcom = comboBox1.Text;
            strbaud = comboBox2.Text;
            strdatabit = comboBox3.Text;
            strparity = comboBox4.Text;
            strstopbit = comboBox5.Text;

            try
            {
                
                
                OpenSerialCom();
             
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开串口失败!");
            }

            UpdateTableInfo("ComSet", "ComPort", comboBox1.Text);
            UpdateTableInfo("ComSet", "Baud", comboBox2.Text);
            UpdateTableInfo("ComSet", "Databits", comboBox3.Text);
            UpdateTableInfo("ComSet", "Parity", comboBox4.Text);
            UpdateTableInfo("ComSet", "Stopbits", comboBox5.Text);
        }
        public void OpenSerialCom()
        {
            try
            {
                ModbusRtu.Close();
                bool bopen = ModbusRtu.Open(strcom, int.Parse(strbaud), int.Parse(strdatabit), (Parity)Enum.Parse(typeof(Parity), strparity, true), (StopBits)Enum.Parse(typeof(StopBits), strstopbit, true));
                if (!bopen)
                {
                    label6.ForeColor = Color.Red;
                    label6.Text = strcom + "打开失败!";
                    m_isPortOpen = false;
                    MessageBox.Show(label6.Text);
                }
                else
                {
                    label6.ForeColor = Color.Green;
                    label6.Text = strcom + "已打开!";
                    m_isPortOpen = true;
                }
            }
            catch (Exception ex)
            {
                label6.ForeColor = Color.Red;
                label6.Text = strcom + "打开失败!";
                MessageBox.Show(label6.Text);
                m_isPortOpen = false;
            }
        }
        public bool Open(string portName, int baudRate, int databits, Parity parity, StopBits stopBits)
        {
            //Ensure port isn't already opened:
            if (!sp.IsOpen)
            {
                //Assign desired settings to the serial port:
                sp.PortName = portName;
                sp.BaudRate = baudRate;
                sp.DataBits = databits;
                sp.Parity = parity;
                sp.StopBits = stopBits;
                //These timeouts are default and cannot be editted through the class at this point:
                //sp.ReadTimeout = 604800;
                sp.ReadTimeout = 1000;
                sp.WriteTimeout = 604800;

                try
                {
                    sp.Open();
                }
                catch (Exception err)
                {
                    //  modbusStatus = "Error opening " + portName + ": " + err.Message;
                    return false;
                }
                // modbusStatus = portName + " opened successfully";
                return true;
            }
            else
            {
                // modbusStatus = portName + " already opened";
                return false;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {

        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            //this.label1.Text = DateTime.Now.ToString("HH:mm:ss");
            
            //DateTime current = DateTime.Now.ToLocalTime();
           // this.label15.Text = current.ToString("yyyy年MM月dd dddd HH时mm分ss秒");

           
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            ushort value333 = 3000;
            short value111 = 32767;
            
            string strTemp = "1.5";

            byte aaa = Convert.ToByte(Convert.ToSingle(strTemp)*10);

            ushort value222 = 300;
            if (value333 == 10)
            {
 
            }
            
            ushort value = 0x01;
            ushort value2 = 0x08;
            value = (ushort)(0x01 << 8 | value2);

          
           // timer1.Interval = 1000;
            //timer1.Tick += new EventHandler(Timer_Tick);
           // timer1.Start();

            if (!InitConnectDatabase()) return;

            comboBox1.Text = GetDataVauleToStep("ComSet", "ComPort");
            comboBox2.Text = GetDataVauleToStep("ComSet", "Baud");
            comboBox3.Text = GetDataVauleToStep("ComSet", "Databits");
            comboBox4.Text = GetDataVauleToStep("ComSet", "Parity");
            comboBox5.Text = GetDataVauleToStep("ComSet", "Stopbits");

            //textBox28.Text = GetDataVauleToStep("ComSet", "Addr1");
            

            m_iDeviceAddr = 1;

            for (int i = 1; i < 101; i++)
            {
                domainUpDown1.Items.Add(i);
            }
            
            strcom = GetDataVauleToStep("ComSet", "ComPort");
            strbaud = GetDataVauleToStep("ComSet", "Baud");
            strdatabit = GetDataVauleToStep("ComSet", "Databits");
            strparity = GetDataVauleToStep("ComSet", "Parity");
            strstopbit = GetDataVauleToStep("ComSet", "Stopbits");

            //m_iDeviceAddr = 12;

            domainUpDown1.SelectedIndex = 0;

            try
            {
                OpenSerialCom();//打开串口
                if (m_isPortOpen)
                {
                    ushort[] registerhold = new ushort[1];
                    bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2000, 1, ref registerhold);//版本
                    if (bhold)
                    {

                        label19.ForeColor = Color.Blue;
                        label19.Text = registerhold[0].ToString();

                    }
                    else
                    {
                        label19.ForeColor = Color.Red;

                        label19.Text = "通信错误,确认地址!";

                    }
                }
       
                /*if (thread_poll != null)
                {
                    if (!thread_poll.IsAlive)
                    {
                        thread_poll = new Thread(new ThreadStart(Poll_Thread));
                        thread_poll.Start();
                    }
                }
                else
                {
                    thread_poll = new Thread(new ThreadStart(Poll_Thread));
                    thread_poll.Start();
                }*/

            }
            catch (Exception ex)
            {
               // MessageBox.Show("打开串口失败!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
                if (m_isPortOpen)
                {

                    ushort[] registerhold = new ushort[1];
                    bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2000, 1, ref registerhold);//版本
                    if (!bhold)
                    {

                        MessageBox.Show("通信错误,请检查地址!");
                        m_Runing = false;
                        return;

                    }

                }
                else
                {
                    MessageBox.Show("串口未打开");
                    m_Runing = false;
                    return;
                }


                if (!m_Runing)
                {
                    button3.Text = "停止测试";
                    m_Runing = true;
                    listView1.Items.Clear();
                    thread_poll = new Thread(new ThreadStart(Poll_Thread));
                    thread_poll.Start();
                }
                else
                {
                    button3.Text = "开始测试";
                    m_Runing = false;
                }
        }
        private void ShowBatModuleAddr()
        {
            ushort[] registerhold = new ushort[2];
            bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2306, 1, ref registerhold);//电池节数
            if (bhold)
            {
                textBox24.Text = registerhold[0].ToString();
            }
            else
            {
                MessageBox.Show("获取数据失败!");
            }
        }
        string  GetIntToWeek(ushort value)
        {
            string strValue = "";
            if (value == 0)
            {
                strValue = "星期日";
            }
            if (value == 1)
            {
                strValue = "星期一";
            }
            if (value == 2)
            {
                strValue = "星期二";
            }
            if (value == 3)
            {
                strValue = "星期三";
            }
            if (value == 4)
            {
                strValue = "星期四";
            }
            if (value == 5)
            {
                strValue = "星期五";
            }
            if (value == 6)
            {
                strValue = "星期六";
            }
            return strValue;
        }
        void GetDeviceAndSververTime(string devTime, string localTime)
        {

            textBox26.Text = devTime;

            textBox27.Text = localTime;

            label39.Text = "获取结束";

            button11.Enabled = true;
            button12.Enabled = true;

            button10.Enabled = true;
            button9.Enabled = true;
        }
        private void Poll_Thread2()
        {

            ushort[] registerhold = new ushort[7];
            bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2100, 7, ref registerhold);//
            string Value = "";
            string Value2 = "";
            if (bhold)
            {

                Value = registerhold[0] + "年" + registerhold[1].ToString("00") + "月" + registerhold[2].ToString("00") + " " + GetIntToWeek(registerhold[3]) + " " + registerhold[4].ToString("00") + "时" + registerhold[5].ToString("00") + "分" + registerhold[6] + "秒 ";
                //textBox26.Text = Value;
            }
            else
            {
                MessageBox.Show("获取数据失败!");
                return;
                //Value = "通信失败!";
            }
            string current = GetBeijingTime();
            if (current == "") GetBeijingTime();
            if (current == "")
            {
                DateTime time = DateTime.Now.ToLocalTime();
                string weekname = GetWeekFromEnToChName(time.DayOfWeek);
                Value2 = time.Year + "年" + time.Month + "月" + time.Day + " " + weekname + " " + time.Hour + "时" + time.Minute + "分" + time.Second + "秒 ";
                //textBox27.Text = Value;
            }
            else
            {
                //DateTime time = Convert.ToDateTime(current).ToString("yyyy-MM-dd HH:mm:ss");
                Value2 = Convert.ToDateTime(current).ToString("yyyy年MM月dd dddd HH时mm分ss秒");
            }
            Invoke(new ShowTimeInfo(GetDeviceAndSververTime), new object[] {Value, Value2});
        }
        private void GetParamTestValue()
        {

            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";

            textBox10.Text = "";
            textBox9.Text = "";
            textBox8.Text = "";

            textBox13.Text = "";
            textBox12.Text = "";
            textBox11.Text = "";

            textBox16.Text = "";
            textBox15.Text = "";
            textBox14.Text = "";


            textBox19.Text = "";
            textBox18.Text = "";
            textBox17.Text = "";

            textBox21.Text = "";
            textBox20.Text = "";

            textBox22.Text = "";

            float value = 0;
            ushort[] registerhold = new ushort[50];
            bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2001, 23, ref registerhold);//电池节数
            if (bhold)
            {
                textBox5.Text = registerhold[3].ToString();
                textBox6.Text = registerhold[4].ToString();
                textBox7.Text = registerhold[5].ToString();

                textBox10.Text = registerhold[6].ToString();

                value = (float)(registerhold[7]) / 10;
                textBox9.Text = value.ToString();

                value = (float)(registerhold[8]) / 10;
                textBox8.Text = value.ToString();

                textBox13.Text = registerhold[9].ToString();

                value = (float)(registerhold[10]) / 10;
                textBox12.Text = value.ToString();
                
                
                textBox11.Text = registerhold[11].ToString();

                textBox16.Text = registerhold[12].ToString();
                textBox15.Text = registerhold[13].ToString();
                textBox14.Text = registerhold[15].ToString();


                textBox19.Text = registerhold[18].ToString();
                textBox18.Text = registerhold[19].ToString();
                textBox17.Text = registerhold[20].ToString();

                textBox21.Text = registerhold[21].ToString();
                textBox20.Text = registerhold[22].ToString();

                textBox22.Text = registerhold[0].ToString();

            }
            else
            {
                MessageBox.Show("获取数据失败!");
            }
             //ushort[] registerhold = new ushort[120];
            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2089, 1, ref registerhold);//电池节数
            if (bhold)
            {
                textBox23.Text = registerhold[0].ToString();
            }
        }
       
        private void Poll_Thread()
        {
           // byte SlaveAddr = 1;
            //ushort naddr = 0;
            ushort[] registerhold = new ushort[8];
            string sVoltValue = "111";
            string sTempValue = "222";
            string sNeizuValue = "333";
            ushort value = 0x01;
            //ushort value2 = 0x08;
            //ushort value3 = 0;
            float value4 = 0;
            string strTotalVol = "";
            string strTotalCur = "";
            string strTemp1 = "";
            string strTemp2 = "";
            string strcount = "";
            string strVersion = "";
            ushort icount = 0;
            ushort[] registerhold2 = new ushort[120];
            int value5 = 0;
            //byte tempvalue=0;
            while (m_Runing)
            {
                /////////////////////读总的信息
                if (m_SelectIndex == 0)
                {
                    bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2001, 1, ref registerhold);//电池节数
                    if (bhold)
                    {
                        value = registerhold[0];
                        icount = value;
                        strcount = value.ToString();
                    }

                    bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2000, 1, ref registerhold);//版本
                    if (bhold)
                    {
                        value = registerhold[0];

                        strVersion = value.ToString();
                    }

                    
                    //////////////////////////////获取时间


               
                    bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2100, 7, ref registerhold);//
                    string DeviceTime = "";
            
                    if (bhold)
                    {

                        DeviceTime = registerhold[0] + "年" + registerhold[1].ToString("00") + "月" + registerhold[2].ToString("00") + " " + GetIntToWeek(registerhold[3]) + " " + registerhold[4].ToString("00") + "时" + registerhold[5].ToString("00") + "分" + registerhold[6] + "秒 ";
                        //textBox26.Text = Value;
                    }
                    else
                    {
                        DeviceTime = "获取时间失败!";
                        //Value = "通信失败!";
                    }
                    
                    ///////////////////////////////
                    bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 1, 4, ref registerhold);//总电压、电流、温度
                    if (bhold)
                    {
                        //sVoltValue = registerhold[0]
                        value = registerhold[0];
                        value4 = (float)(value) / 10;
                        strTotalVol = value4.ToString();

                        value = registerhold[1];
                        if (value > 32767)
                        {
                            value5 = (value - 65536);
                            value4 = (float)(value5) / 10;
                        }
                        else
                        {
                            value4 = (float)(value) / 10;
                        }
                        strTotalCur = value4.ToString();

                        value = registerhold[2];
                        if (value > 32767)
                        {
                            value5 = (value - 65536);
                            value4 = (float)(value5) / 10;
                        }
                        else
                        {
                            value4 = (float)(value) / 10;
                        }
                        strTemp1 = value4.ToString();

                        value = registerhold[3];
                        if (value > 32767)
                        {
                            value5 = (value - 65536);
                            value4 = (float)(value5) / 10;
                        }
                        else
                        {
                            value4 = (float)(value) / 10;
                        }
                        strTemp2 = value4.ToString();

                        Invoke(new ShowTestInfo(ShowTestInfoToUser), new object[] { strTotalVol, strTotalCur, strTemp1, strTemp2, strcount, strVersion, DeviceTime });
                    }
                    else
                    {
                        Invoke(new ShowTestInfo(ShowTestInfoToUser), new object[] { strTotalVol, strTotalCur, strTemp1, strTemp2, strcount, "通信错误!", DeviceTime });
                        //m_Runing = false;
                        //MessageBox.Show("通信断开,请重新点击开始");
                        //return;
                        Thread.Sleep(1000);
                        continue;
                    }

                    /////////////////////
                    ////////////////////

                    //icount = 2;
                    Invoke(new ConfirmListCount(ConfirmDeleteList), new object[] { (byte)(icount) });
                    Thread.Sleep(20);
                    //if (listView1.Items.Count == 0)
                    {

                        if (icount <= 40)
                        {
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 50, (ushort)(3 * icount), ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < icount; i++)
                                {
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] { sVoltValue, sTempValue, sNeizuValue, i });
                                }
                            }
                        }
                        else if (icount > 40 && icount <= 80)
                        {
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 50, 120, ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < 40; i++)
                                {
                                    //if(i==
                                    
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] { sVoltValue, sTempValue, sNeizuValue, i });
                                }
                            }
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 170, (ushort)((icount - 40) * 3), ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < icount-40; i++)
                                {
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    
                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] {sVoltValue, sTempValue, sNeizuValue, (byte)(40 + i) });
                                }
                            }

                        }
                        else if (icount > 80)
                        {
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 50, 120, ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < 40; i++)
                                {
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] { sVoltValue, sTempValue, sNeizuValue, i });
                                }
                            }
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 170, 120, ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < 40; i++)
                                {
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] { sVoltValue, sTempValue, sNeizuValue, (byte)(40 + i) });
                                }
                            }
                            bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 290, (ushort)((icount - 80) * 3), ref registerhold2);
                            if (bhold)
                            {
                                for (byte i = 0; i < icount-80; i++)
                                {
                                    value = registerhold2[3 * i];
                                    value4 = (float)(value) / 1000;
                                    sVoltValue = value4.ToString();

                                    value = registerhold2[3 * i + 1];
                                    if (value > 32767)
                                    {
                                        value5 = (value - 65536);
                                        value4 = (float)(value5) / 10;
                                    }
                                    else
                                    {
                                        value4 = (float)(value) / 10;
                                    }
                                    sTempValue = value4.ToString();

                                    value = registerhold2[3 * i + 2];
                                    value4 = (float)(value) / 1000;
                                    sNeizuValue = value4.ToString();

                                    Invoke(new InsertTestRecordToList(InsertTestResult), new object[] { sVoltValue, sTempValue, sNeizuValue, (byte)(80 + i) });
                                }
                            }
                        }

                    }

                }
                else if (m_SelectIndex == 1)
                {

                    // GetParamTestValue();

                }



                Thread.Sleep(1000);
                //////////////////


            }
           

             //   
            //}

        }
        private void ShowTestInfoToUser(string volt, string curr, string temp1, string temp2, string count, string version,string strDeviceTime)
        {
            textBox1.Text = curr;
            textBox2.Text = volt;
            textBox3.Text = temp1;
            textBox4.Text = temp2;
            label18.Text = count;
            label15.Text = strDeviceTime;
            int pos = version.IndexOf("错误");
            if (pos >= 0)
            {
                label19.ForeColor = Color.Red;
                label15.ForeColor = Color.Red;
                //m_bStart = false;
                //button3.Enabled = true;
                button3.Text = "开始检测";
                
            }
            else
            {
                label19.ForeColor = Color.Blue;
                label15.ForeColor = Color.Blue;
                //button3.Enabled = false;
                button3.Text = "停止检测";
            }
            label19.Text = version;
            //listView1.Items.Clear();
           
        }
        private void InsertTestResult(string volt, string temp,string neizu,byte index)
        {

            if (listView1.Items.Count < index + 1)
            {

                ListViewItem OptionItem = new ListViewItem((index + 1).ToString());

                OptionItem.UseItemStyleForSubItems = false;
                OptionItem.SubItems.Add(volt);//
                OptionItem.SubItems.Add(temp);//
                OptionItem.SubItems.Add(neizu);//
                listView1.Items.Add(OptionItem);//执行添加操作
            }
            else
            {
                listView1.Items[index].SubItems[1].Text = volt;
                listView1.Items[index].SubItems[2].Text = temp;
                listView1.Items[index].SubItems[3].Text = neizu;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                if (tabControl1.SelectedTab.Name == "tabPage1")
                {
                    //MessageBox.Show("实时数据");
                    m_SelectIndex = 0;
                    if (!m_isPortOpen)
                    {
                        MessageBox.Show("串口未打开");
                    }
                   
                }
                else if (tabControl1.SelectedTab.Name == "tabPage2")
                {
                    //MessageBox.Show("参数设置");
                    //tabControl1.SelectedIndex = 0;
                    m_SelectIndex = 1;
                    if (m_isPortOpen)
                    {
                        GetParamTestValue();
                    }
                    else
                    {
                        MessageBox.Show("串口未打开");
                    }
                }
                else if (tabControl1.SelectedTab.Name == "tabPage3")
                {
                    //MessageBox.Show("参数设置");
                    //tabControl1.SelectedIndex = 0;
                    m_SelectIndex = 2;
                    if (m_isPortOpen)
                    {
                        ShowBatModuleAddr();
                    }
                    else
                    {
                        MessageBox.Show("串口未打开");
                    }
                }
                else if (tabControl1.SelectedTab.Name == "tabPage4")
                {
                    //MessageBox.Show("参数设置");
                    m_SelectIndex = 3;
                    //tabControl1.SelectedIndex = 0;
                    button11.Enabled = false;
                    button12.Enabled = false;
                    button10.Enabled = false;
                    button9.Enabled = false;
                    if (m_isPortOpen)
                    {
                        GetDeviceTestModeInfo();

                        label39.Text = "正在获取时间...";
                        textBox26.Text = "";

                        textBox27.Text = "";
                        thread_poll2 = new Thread(new ThreadStart(Poll_Thread2));
                        thread_poll2.Start();
                    }
                    else
                    {
                        MessageBox.Show("串口未打开");
                    }
                    //GetDeviceAndSververTime(); 
                }
            }
            catch (Exception ex)
            {
 
            }
            
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            GetParamTestValue();
        }

        private void button7_Click(object sender, EventArgs e)
        {

            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }

            if (textBox36.Text == "")
            {
                MessageBox.Show("设置值不能为空");
                return;
            }

            ushort[] registerhold = new ushort[1];

            registerhold[0] = Convert.ToUInt16(textBox36.Text);

            bool bhold = ModbusRtu.SendFc16(m_iDeviceAddr, 2089, 1, registerhold);//电池节数
            
            if (bhold)
            {
                MessageBox.Show("设置成功");
                m_iDeviceAddr =(byte)registerhold[0];
                GetParamTestValue();
            }
            else
            {
                MessageBox.Show("设置失败");
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            label39.Text = "正在获取时间...";
            textBox26.Text = "";
            textBox27.Text = "";
            thread_poll2 = new Thread(new ThreadStart(Poll_Thread2));
            thread_poll2.Start();
        }
        private string GetCurrDevTestMode(ushort[] registerhold)
        {
            string strValue = "";

            switch (registerhold[0])
            { 
                case 0:
                    strValue = "模式0，关闭内阻定时测量功能";
                    label48.Text = "";
                    break;
                case 1:
                    strValue = "模式1，每N分钟测量一次内阻";
                    label48.Text = "内阻测量计时间隔:" + registerhold[5]+"分钟";
                    break;
                case 2:
                    strValue = "模式2，每N小时测量一次内阻";
                    label48.Text = "内阻测量计时间隔:" + registerhold[6] + "小时 实时" + registerhold[6]+"分钟";
                    break;
                case 3:
                    strValue = "模式3，每天测量一次内阻";
                    label48.Text = "每天:" + registerhold[3] + "时" + registerhold[4] + "分"; ;
                    break;
                case 4:
                    strValue = "模式4，每周测量一次内阻";
                    if (registerhold[1] == 0)
                    {
                        label48.Text = "每周日: " + registerhold[3] + "时" + registerhold[4] + "分";
                    }
                    else
                    {
                        label48.Text = "每周:" + registerhold[1] +" " + registerhold[3] + "时" + registerhold[4] + "分";
                    }
                    break;
                case 5:
                    strValue = "模式5，每月测量一次内阻";
                    label48.Text = "每月:" + registerhold[2] + "日" + registerhold[3] + "时" + registerhold[4] + "分";
                    break;
                /* 您可以有任意数量的 case 语句 */
                default: /* 可选的 */
                    strValue = "";
                    break;
            }
            return strValue;
        }
        private void GetDeviceTestModeInfo()
        {
            ushort[] registerhold = new ushort[7];
            bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2150, 7, ref registerhold);//电池节数
            if (bhold)
            {
                label40.Text = GetCurrDevTestMode(registerhold);


                comboBox6.SelectedIndex = registerhold[0];
                //comboBox6.Text = registerhold[0].ToString();
                textBox25.Text = registerhold[1].ToString();
                textBox29.Text = registerhold[2].ToString();
                textBox30.Text = registerhold[3].ToString();
                textBox31.Text = registerhold[4].ToString();
                textBox32.Text = registerhold[5].ToString();
                textBox33.Text = registerhold[6].ToString();

            }
        }
        private void button12_Click(object sender, EventArgs e)
        {
            GetDeviceTestModeInfo();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int value = comboBox6.SelectedIndex;

            /*if (value == 2)
            {
 
            }*/

            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }
    
            if (comboBox6.Text == "")
            {
                MessageBox.Show("设置值不能为空");
                return;
            }

            if (textBox25.Text == "")
            {
                MessageBox.Show("周值不能为空");
                return;
            }
            if (textBox29.Text == "")
            {
                MessageBox.Show("日值不能为空");
                return;
            }
            if (textBox30.Text == "")
            {
                MessageBox.Show("时值不能为空");
                return;
            }
            if (textBox31.Text == "")
            {
                MessageBox.Show("分值不能为空");
                return;
            }
            if (textBox32.Text == "")
            {
                MessageBox.Show("内阻测量间隔分钟不能为空");
                return;
            }
            if (textBox33.Text == "")
            {
                MessageBox.Show("内阻测量间隔小时不能为空");
                return;
            }

            ushort[] registerhold = new ushort[7];

            registerhold[0] = (ushort)value;
            registerhold[1] = Convert.ToByte(textBox25.Text);
            registerhold[2] = Convert.ToByte(textBox29.Text);
            registerhold[3] = Convert.ToByte(textBox30.Text);
            registerhold[4] = Convert.ToByte(textBox31.Text);
            registerhold[5] = Convert.ToByte(textBox32.Text);
            registerhold[6] = Convert.ToByte(textBox33.Text);


            bool bhold=ModbusRtu.SendFc16(m_iDeviceAddr, 2150, 7,registerhold);//电池节数
            if (bhold)
            {
                MessageBox.Show("设置成功");
                GetDeviceTestModeInfo();
            }
            else
            {
                MessageBox.Show("设置失败");
            }
            //registerhold
        }
        private  ushort GetWeekName(DayOfWeek week)
        {
            ushort weekvalue = 0;
            switch (week)
            {
                case DayOfWeek.Monday:
                    weekvalue = 1;
                    break;
                case DayOfWeek.Tuesday:
                    weekvalue = 2;
                    break;
                case DayOfWeek.Wednesday:
                    weekvalue = 3;
                    break;
                case DayOfWeek.Thursday:
                    weekvalue = 4;
                    break;
                case DayOfWeek.Friday:
                    weekvalue =5;
                    break;
                case DayOfWeek.Saturday:
                    weekvalue = 6;
                    break;
                case DayOfWeek.Sunday:
                    weekvalue = 0;
                    break;
            }
            return weekvalue;
        }
        private string GetWeekFromEnToChName(DayOfWeek week)
        {
            string name = "";
            switch (week)
            {
                case DayOfWeek.Monday:
                    name = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    name = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    name = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    name = "星期四";
                    break;
                case DayOfWeek.Friday:
                    name = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    name = "星期六";
                    break;
                case DayOfWeek.Sunday:
                    name = "星期日";
                    break;
            }
            return name;
        }
        private void button10_Click(object sender, EventArgs e)
        {

            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }
            
            ushort[] registerhold = new ushort[7];
            DateTime time;

            string current = GetBeijingTime();
            if (current == "") GetBeijingTime();
            if (current == "")
            {
                time = DateTime.Now.ToLocalTime();
                //Value2 = time.Year + "年" + time.Month + "月" + time.Day + " " + time.DayOfWeek + " " + time.Hour + "时" + time.Minute + "分" + time.Second + "秒 ";
                //textBox27.Text = Value;
            }
            else
            {
                 time = Convert.ToDateTime(current);
                //Value2 = Convert.ToDateTime(current).ToString("yyyy年MM月dd dddd HH时mm分ss秒");
            }

            registerhold[0] = (ushort)time.Year;
            registerhold[1] = (ushort)time.Month;
            registerhold[2] = (ushort)time.Day;
            registerhold[3] = GetWeekName(time.DayOfWeek);
            registerhold[4] = (ushort)time.Hour;
            registerhold[5] = (ushort)time.Minute;
            registerhold[6] = (ushort)time.Second;

            bool bhold = ModbusRtu.SendFc16(m_iDeviceAddr, 2100, 7, registerhold);//电池节数
            if (bhold)
            {
                MessageBox.Show("设置成功");
            }
            else
            {
                MessageBox.Show("设置失败");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {

            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }
            if (textBox34.Text == "")
            {
                MessageBox.Show("设置值不能为空");
                return;
            }
            ushort[] registerhold = new ushort[1];

            
            registerhold[0] =(ushort)( 0xFA00| Convert.ToByte(textBox34.Text));
     

            bool bhold = ModbusRtu.SendFc6(m_iDeviceAddr, 2306,1, registerhold);//电池节数

            if (bhold)
            {
                MessageBox.Show("设置成功");
                ShowBatModuleAddr();
            }
            else
            {
                MessageBox.Show("设置失败");
            }
                
            
        }

        private void button13_Click(object sender, EventArgs e)
        {
            ShowBatModuleAddr();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }
            if (textBox35.Text == "")
            {
                MessageBox.Show("设置值不能为空");
                return;
            }
            ushort[] registerhold = new ushort[1];

            registerhold[0] = Convert.ToUInt16(textBox35.Text);
           
            bool bhold = ModbusRtu.SendFc16(m_iDeviceAddr, 2001, 1, registerhold);//电池节数
            if (bhold)
            {
                MessageBox.Show("设置成功");
                GetParamTestValue();
            }
            else
            {
                MessageBox.Show("设置失败");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            if (!m_DebugRight)
            {
                MessageBox.Show("没有修改权限");
                return;
            }

            if (textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "")
            {
                MessageBox.Show("设置值不能为空!");
                return;
            }

            if (textBox10.Text == "" || textBox11.Text == "" || textBox12.Text == "" || textBox13.Text == "" || textBox14.Text == "")
            {
                MessageBox.Show("设置值不能为空!");
                return;
            }

            if (textBox15.Text == "" || textBox16.Text == "" || textBox17.Text == "" || textBox18.Text == "" || textBox19.Text == "" || textBox20.Text == "" || textBox21.Text == "")
            {
                MessageBox.Show("设置值不能为空!");
                return;
            }


            ushort[] registerhold = new ushort[11];

            registerhold[0] = Convert.ToUInt16(textBox5.Text);
            registerhold[1] = Convert.ToUInt16(textBox6.Text);
            registerhold[2] = Convert.ToUInt16(textBox7.Text);

            registerhold[3] = Convert.ToUInt16(textBox10.Text);

            registerhold[4] = Convert.ToUInt16(Convert.ToSingle(textBox9.Text) * 10);
            registerhold[5] = Convert.ToUInt16(Convert.ToSingle(textBox8.Text) * 10);

            registerhold[6] = Convert.ToUInt16(textBox13.Text);

            registerhold[7] = Convert.ToUInt16(Convert.ToSingle(textBox12.Text) * 10);

            registerhold[8] = Convert.ToUInt16(textBox11.Text);


            registerhold[9] = Convert.ToUInt16(textBox16.Text);
            registerhold[10] = Convert.ToUInt16(textBox15.Text);


            bool bhold = ModbusRtu.SendFc16(m_iDeviceAddr, 2004, 11, registerhold);//电池节数

            Thread.Sleep(100);
            //////////////////
            ushort[] registerhold2 = new ushort[1];


            registerhold2[0] = Convert.ToUInt16(textBox14.Text);


            bhold = ModbusRtu.SendFc6(m_iDeviceAddr, 2016, 1, registerhold2);//电池节数

            ////////////////////////////
            registerhold[0] = Convert.ToUInt16(textBox19.Text);
            registerhold[1] = Convert.ToUInt16(textBox18.Text);
            registerhold[2] = Convert.ToUInt16(textBox17.Text);

            registerhold[3] = Convert.ToUInt16(textBox21.Text);
            registerhold[4] = Convert.ToUInt16(textBox20.Text);

            Thread.Sleep(100);
            bhold = ModbusRtu.SendFc16(m_iDeviceAddr, 2019, 5, registerhold);//电池节数
            if (bhold)
            {
                MessageBox.Show("设置成功");
                GetParamTestValue();
            }
            else
            {
                MessageBox.Show("设置失败");
            }

        }

        

        private void button5_Click(object sender, EventArgs e)
        {
            
            
            ushort[] registerhold = new ushort[1];


            //registerhold[0] = (ushort)(0xFA00 | Convert.ToByte(textBox34.Text));
            registerhold[0] = (ushort)0x5A5A;

            bool bhold = ModbusRtu.SendFc6(m_iDeviceAddr, 2302, 1, registerhold);//电池节数

            if (bhold)
            {
                MessageBox.Show("设置成功");
                //ShowBatModuleAddr();
            }
            else
            {
                MessageBox.Show("设置失败");
            }
        }

       

        private void button14_Click_1(object sender, EventArgs e)
        {

            
            
            if (m_isPortOpen)
            {
                button12.Enabled = false;

                ushort[] registerhold = new ushort[1];
                bool bhold = ModbusRtu.SendFc3(m_iDeviceAddr, 2000, 1, ref registerhold);//版本
                if (bhold)
                {

                    label19.ForeColor = Color.Blue;
                    label19.Text = registerhold[0].ToString();

                }
                else
                {
                    label19.ForeColor = Color.Red;

                    label19.Text = "通信错误,确认地址!";

                }
                button12.Enabled = true;
            }
            else
            {
                MessageBox.Show("串口未打开");
            }
        }

        private void 设置登陆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 Dessetdlg = new Form2();
            DialogResult resut;
            resut = Dessetdlg.ShowDialog();

            if (resut == DialogResult.OK)
            {
                //Application.Run(new Form1(Dessetdlg.iLogType, Dessetdlg.m_user));
                //Application.Run(new Form1());
                string strValue = this.Text;
                strValue=strValue.Replace("操作员", "管理员");
                this.Text = strValue;
                m_DebugRight = true;
            }
            
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                m_Runing = false;
                if (m_isPortOpen)
                    ModbusRtu.Close();
            }
            catch (Exception ex)
            { 

            }

        }
        private void ConfirmDeleteList(byte count)
        {
            if (listView1.Items.Count > count)
            {
                listView1.Items.Clear();
            }
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            int seleindex = comboBox6.SelectedIndex;
            

            if (seleindex == 0)//
            {
                textBox25.Visible = false;  //周
                textBox29.Visible = false;  //日
                textBox30.Visible = false;  //时
                textBox31.Visible = false;  //分
                textBox32.Visible = false;  //内阻测量计时间隔-分钟
                textBox33.Visible = false;  //内阻测量计时间隔-小时

                label42.Visible = false;
                label43.Visible = false;
                label44.Visible = false;
                label45.Visible = false;
                label46.Visible = false;
                label47.Visible = false;
            }
            else if (seleindex == 1)//模式1
            {
                textBox25.Visible = false;  //周
                textBox29.Visible = false;  //日
                textBox30.Visible = false;  //时
                textBox31.Visible = false;  //分
                textBox32.Visible = true;  //内阻测量计时间隔-分钟
                textBox33.Visible = false;  //内阻测量计时间隔-小时

                label42.Visible = false;
                label43.Visible = false;
                label44.Visible = false;
                label45.Visible = false;
                label46.Visible = true;
                label47.Visible = false;
            }
            else if (seleindex == 2)//模式2
            {
                textBox25.Visible = false;  //周
                textBox29.Visible = false;  //日
                textBox30.Visible = false;  //时
                textBox31.Visible = false;  //分
                textBox32.Visible = true;  //内阻测量计时间隔-分钟
                textBox33.Visible = true;  //内阻测量计时间隔-小时

                label42.Visible = false;
                label43.Visible = false;
                label44.Visible = false;
                label45.Visible = false;
                label46.Visible = true;
                label47.Visible = true;
            }
            else if (seleindex == 3)//模式3
            {
                textBox25.Visible = false;  //周
                textBox29.Visible = false;  //日
                textBox30.Visible = true;  //时
                textBox31.Visible = true;  //分
                textBox32.Visible = false; //内阻测量计时间隔-分钟
                textBox33.Visible = false; //内阻测量计时间隔-小时

                label42.Visible = false;
                label43.Visible = false;
                label44.Visible = true;
                label45.Visible = true;
                label46.Visible = false;
                label47.Visible = false;
            }
            else if (seleindex == 4)//模式4
            {
                textBox25.Visible = true;  //周
                textBox29.Visible = false;  //日
                textBox30.Visible = true;  //时
                textBox31.Visible = true;  //分
                textBox32.Visible = false;  //内阻测量计时间隔-分钟
                textBox33.Visible = false;  //内阻测量计时间隔-小时

                label42.Visible = true;
                label43.Visible = false;
                label44.Visible = true;
                label45.Visible = true;
                label46.Visible = false;
                label47.Visible = false;
            }
            else if (seleindex == 5)//模式5
            {
                textBox25.Visible = false;  //周
                textBox29.Visible = true;  //日
                textBox30.Visible = true;  //时
                textBox31.Visible = true;  //分
                textBox32.Visible = false; //内阻测量计时间隔-分钟
                textBox33.Visible = false; //内阻测量计时间隔-小时

                label42.Visible = false;
                label43.Visible = true;
                label44.Visible = true;
                label45.Visible = true;
                label46.Visible = false;
                label47.Visible = false;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            m_Runing = false;
            if (m_isPortOpen)
            {
                ModbusRtu.Close();
                m_isPortOpen = false;
                label6.ForeColor = Color.Red;
                label6.Text = strcom + "已关闭!";
            }
        }
        bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是，就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是，就返回False
                }
            }
            return true;                                        //是，就返回True
        }
        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            try
            {
                if (domainUpDown1.Text != "")
                {
                    if (!IsNumeric(domainUpDown1.Text))
                    {
                        //MessageBox.Show("必须为数字!");
                    }
                    else
                    {
                        m_iDeviceAddr = Convert.ToByte(domainUpDown1.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("范围0-255");
                domainUpDown1.Text = "";
            }
            //m_iDeviceAddr = Convert.ToByte(domainUpDown1.Text);
           // domainUpDown1.Text = "";
        }

        private void textBox35_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox36_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox10_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox13_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox11_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox16_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox15_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox14_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox19_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox18_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox17_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox21_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox20_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox34_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox25_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox29_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox30_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox31_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox32_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void textBox33_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

        private void domainUpDown1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
            {
                //if (e.KeyChar == '.') return;

                e.Handled = true;
                // MessageBox.Show("dd");
            }
            else
            {

            }
        }

       
            
    }
}
